package com.blueocean_health.care.management.controller;
/**
 * 订单流水表接口 2017年8月23日18:06:20
 * @author wuyanli
 *
 */

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.blueocean_health.care.management.common.base.em.TimeType;
import com.blueocean_health.care.management.common.base.string.StringUtils;
import com.blueocean_health.care.management.common.base.time.TimeUtils;
import com.blueocean_health.care.management.common.excel.ExcelUtil;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.dto.OrderFlowDto;
import com.blueocean_health.care.management.service.OrderChargeInfoService;
import com.github.pagehelper.PageInfo;
/**
 * 订单流水列表  接口
 * @author wuyanli
 *
 */
@RestController
@RequestMapping("web/order/fee")
public class OrderChargeInfoController {
	
	@Resource
	private OrderChargeInfoService orderChargeService;

	/**
	 * 根据筛选条件查询订单流水列表 2017年8月23日18:23:27
	 * @param map
	 * @return
	 */
	@RequestMapping("list")
	public SysResult getOrderFlowList(@RequestBody Map<String,String> map){
		PageInfo<OrderFlowDto> orderFlowList = orderChargeService.getOrderFlowList(map);
		Map<String,Object> resultMap = new HashMap<>();
		Map<String,Object> pageMap = new HashMap<>();
		Map<String, Object> countMap = orderChargeService.getAmountCount(map);
		resultMap.put("list", orderFlowList.getList());
		pageMap.put("total", orderFlowList.getTotal());
		resultMap.put("amountCount", countMap.get("amountCount"));
		resultMap.put("totalPayCount", countMap.get("totalPayCount"));
		resultMap.put("pageInfo", pageMap);
		return SysResult.ok(0, "success",resultMap);
	}
	/**
	 * 导出Excel 订单流水列表 2017年8月25日15:23:19
	 * @param map
	 * @return
	 */
	@RequestMapping("orderFlowExcel")
	public void orderFlowExcel(HttpServletRequest request, HttpServletResponse response){
		try{
			Map<String,String> map = new HashMap<>();
			if(!StringUtils.isEmpty(request.getParameter("minReceiptNumber"))){
				map.put("minReceiptNumber", request.getParameter("minReceiptNumber").trim());
			}else{
				map.put("minReceiptNumber", request.getParameter("minReceiptNumber"));
			}
			if(!StringUtils.isEmpty(request.getParameter("maxReceiptNumber"))){
				map.put("maxReceiptNumber", request.getParameter("maxReceiptNumber").trim());
			}else{
				map.put("maxReceiptNumber", request.getParameter("maxReceiptNumber"));
			}
			if(!StringUtils.isEmpty(request.getParameter("orderId"))){
				map.put("orderId", request.getParameter("orderId").trim());
			}else{
				map.put("orderId", request.getParameter("orderId"));
			}
			if(!StringUtils.isEmpty(request.getParameter("workerName"))){
				map.put("workerName", request.getParameter("workerName").trim());
			}else{
				map.put("workerName", request.getParameter("workerName"));
			}
			map.put("startTime", request.getParameter("startTime"));
			map.put("endTime", request.getParameter("endTime"));
			map.put("feeType", request.getParameter("feeType"));
			map.put("transType", request.getParameter("transType"));
			map.put("amountMin", request.getParameter("amountMin"));
			map.put("amountMax", request.getParameter("amountMax"));
			map.put("amountCount", request.getParameter("amountCount"));
//			map.put("receiptNumber", request.getParameter("receiptNumber"));
			map.put("userId", request.getParameter("userId"));
			map.put("timeType", request.getParameter("timeType"));
			List<OrderFlowDto> allOrderFlowExcel = orderChargeService.getAllOrderFlowExcel(map);
			//导出excel 总金额
//			OrderFlowDto amountCountDto = new OrderFlowDto();
//			String amountCount2 = orderChargeService.getAmountCount(map);
//			amountCountDto.setAmount("金额总数:"+amountCount2+" 元");
//			allOrderFlowExcel.add(amountCountDto);
			String finalFileName = URLEncoder.encode("订单流水表_"+TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS)+".xls", "UTF8");
			response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			
			ExcelUtil.getInstance().exportObj2Excel(toClient, allOrderFlowExcel, OrderFlowDto.class);
		}catch(IOException e){
			e.printStackTrace();
		}
		
	}
}
